<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="12"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="loginForm"
          :rules="rules"
          ref="loginRef"
        >
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              :prefix-icon="User"
              type="text"
            />
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            />
          </el-form-item>
          <el-button
            :loading="loading"
            class="login_button"
            type="primary"
            @click="login"
            >登陆</el-button
          >
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
  import { ref, reactive } from 'vue'
  import { User, Lock } from '@element-plus/icons-vue'
  // 引入提示
  import { ElNotification } from 'element-plus'
  // 引入路由器
  import { useRouter, useRoute } from 'vue-router'
  let $router = useRouter()
  let $route = useRoute()

  //引入pinia的user小仓库函数进行登陆请求
  import useUserStore from '@/store/modules/user'
  // 引入获取早晚上工具类
  import { getTimeMsg } from '@/utils/times'
  let userStore = useUserStore()
  // 表单数据
  let loginForm = reactive({
    username: 'admin',
    password: '111111'
  })
  // 拿到对应的结构
  const loginRef = ref()
  // 控制loading
  let loading = ref(false)
  // 登陆回调
  // 技巧用trycatch包围，不用then，直接捕获Promise异常
  // async使得异步变成同步
  const login = async () => {
    await loginRef.value.validate()
    try {
      loading.value = true
      // await 会让代码在这里暂停执行，直到 userStore.userLogin(loginForm) 返回的 Promise 被解析（成功）或拒绝（失败）。
      await userStore.userLogin(loginForm)
      // console.log($route)
      $router.push({ path: <string>$route.query.redirect || '/' })
      //跳转
      ElNotification({
        type: 'success',
        message: '登陆成功！',
        title: getTimeMsg() + '好!'
      })
      // $router.push('/')
      loading.value = false
    } catch (error) {
      loading.value = false
      // console.log(error)
      ElNotification({
        type: 'error',
        message: (error as Error).message
      })
    }
  }

  // 校验规则
  let rules = {
    username: [
      {
        required: true,
        min: 5,
        max: 10,
        message: '账号6-10位之间',
        trigger: 'change'
      }
    ],
    password: [
      {
        required: true,
        min: 6,
        max: 10,
        message: '密码6-10位之间',
        trigger: 'change'
      }
    ]
  }
</script>

<style lang="scss" scoped>
  .login_container {
    width: 100%;
    height: 100vh;
    background-color: red;
    background-image: url('@/assets/images/background.jpg');
    background-repeat: no-repeat;
    background-size: cover;
    .login_form {
      border-radius: 5px;
      width: 70%;
      top: 30vh;
      position: relative;
      background-image: url('@/assets/images/login_form.png');
      background-size: cover;
      padding: 50px;
      h1 {
        color: white;
        font-size: 40px;
      }
      h2 {
        font-size: 20px;
        color: white;
        margin: 30px 0;
      }
      .login_button {
        width: 100%;
      }
    }
  }
</style>
